home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / st.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  19KB  |  960 lines

  1. /* OS- and machine-dependent stuff for Atari-ST
  2.  * Adapted from the PC version to compile under Lattice C
  3.  * by Walter Doerr, DG2KK (dg2kk@cup.portal.com)
  4.  *
  5.  * 20-2-88:    added code from the Atari MWC version by Rob Janssen PE1CHL 
  6.  * 13-1-88:    added code needed for the 871225.1 version
  7.  * 24-12-87:    first version, adapted from PC.C from the 870412 release
  8.  */
  9.  
  10. #include "stdio.h"
  11. #include "global.h"
  12. #include "config.h"
  13. #include "mbuf.h"
  14. #include "internet.h"
  15. #include "iface.h"
  16. #include "st.h"
  17. #include "cmdparse.h"
  18. #include "asy.h"
  19.  
  20. /* #include "stdlib.h"    chkml() , malloc , free */
  21.  
  22. #ifdef LATTICE
  23. #include "dos.h"    /* dfind */
  24. #endif
  25. #include "osbind.h"
  26.  
  27. #ifdef    MWC
  28. #include <stat.h>
  29. #include <path.h>
  30. #include <time.h>
  31. #include <signal.h>
  32. FILE *stdprt;
  33. extern    char **environ;
  34. #endif
  35.  
  36. #define TRUE -1
  37. #define FALSE 0
  38.  
  39. struct asy asy[ASY_MAX];
  40.  
  41. /* Interface list header */
  42. struct interface *ifaces;
  43.  
  44. static struct TempList {
  45.     struct TempList *next;
  46.     char *name;
  47. } *Head;
  48.  
  49. unsigned nasy;            /* needed in v871225.1 */
  50.  
  51. char *ttbuf;
  52. char *rsbuf;            /* location of memory alloc'd for rs232 buf */
  53.  
  54. #ifdef SCREEN
  55. char *newscreen, *newscradr;    /* 32k memory allocated to 2nd video screen */
  56. long logscreen, physcreen;
  57. int toggle = 0;            /* toggle between normal and trace screen */
  58. int printtrace = 0;        /* send trace to printer (controlled by F2) */
  59. #endif
  60.  
  61. char *shell,*getenv();        /* Name of command shell for exec */
  62. int    rows=25;        /* Number of text rows on screen (may be 50) */
  63.  
  64. /* called during init b4 anything is printed on the screen.
  65.  * PC.C does a lot of memory allocation stuff here.
  66.  * We just set the cursor to "blink" and allocate memory for a second video
  67.  * screen.
  68.  */
  69. ioinit()
  70. {
  71.     char *lmalloc();    /* Takes a long arg */
  72.     unsigned long ptr;
  73.  
  74. #ifdef SCREEN
  75.     newscradr = lmalloc(32*1024L);    /* allocate 32k for 2nd video screen */
  76.     ptr = (unsigned long)newscradr;
  77.     newscreen = (char *)((ptr+255) & 0xffffff00L);    /* 256 byte boundary */
  78.  
  79.     physcreen = (long)Physbase();    /* remember displayed screen address */
  80.     logscreen = (long)Logbase();    /* remember output screen address */
  81.     Cconws("\033j");        /* Save cursor position */
  82.     (void) Setscreen(newscreen,-1L,-1);    /* switch to 2nd screen */
  83.     (void) Vsync();            /* wait for vsync */
  84.     Cconws("\033H\033J");        /* cursor home, clear screen */
  85.     Cconws("\033k");        /* restore cursor position */
  86.     (void) Setscreen(logscreen,-1L,-1);    /* restore old screen */
  87.     (void) Vsync();            /* wait for vsync */
  88. #endif
  89.     printf("\033v\033e\n");        /* Autowrap on, Cursor on */
  90.     (void) Cursconf(2,0);        /* make the cursor blink (3=steady) */
  91. #ifdef MWC
  92.     stdprt = fopen("prn:","w");
  93.     signal(SIGINT, SIG_IGN);    /* Ignore ^C in GEMDOS I/O  -- hyc */
  94. #endif
  95.  
  96.     shell = getenv("NROWS");
  97.     if (shell != NULL)
  98.         rows=atoi(shell);
  99.  
  100.     shell = getenv("SHELL");
  101.     if (shell == NULL)
  102.         shell="\\bin\\gulam.prg";
  103. }
  104.  
  105. /* Called just before exiting. 
  106.  * delete temp files.
  107.  * free memory allocated to the rs-232 and midi buffers.
  108.  */
  109. iostop()
  110. {
  111.     register struct TempList *tptr;
  112.  
  113.     /* free memory allocated to 2nd video screen */
  114.  
  115. #ifdef SCREEN
  116.     dispscreen(0);        /* switch back to original screen */
  117.     free(newscradr);
  118. #endif
  119.  
  120. #ifndef    MWC
  121.     /* delete all temp files that have accumulated */
  122.     (void) tmpdel();
  123. #endif
  124.  
  125.     /* free memory allocated to RS-232/MIDI I/O buffers */
  126.     while (ifaces != NULLIF) {
  127.         if (ifaces->stop != NULLFP)
  128.             (*ifaces->stop)(ifaces);
  129.         ifaces = ifaces->next;
  130.     }
  131.  
  132.     tptr = Head;            /* Delete all temp files */
  133.     while( tptr != NULL) {
  134.         if(tptr->name != NULL)
  135.             unlink(tptr->name);
  136.         tptr = tptr->next;
  137.     }
  138.     printf("\n");
  139. }
  140.  
  141. #ifndef    MWC
  142. /* checks the time then ticks and updates ISS */
  143. static int32 clkval = 0;
  144. void
  145. check_time()
  146. {
  147.     int32 iss();            /* initial sequence number */
  148.     int32 clksec();
  149.     if(clkval != clksec()){
  150.          clkval = clksec();
  151.         icmpclk();
  152.         tick();
  153.         (void)iss();
  154.     }
  155. }
  156.  
  157. /* returns the number of seconds from system clock */
  158. static int32
  159. clksec()
  160. {
  161.     long tloc;
  162.     time(&tloc);
  163.     return (tloc);
  164. }
  165. #else
  166. static    clock_t    clkval=0;
  167. void
  168. check_time()
  169. {
  170.     int32 iss();
  171.  
  172.     /* System clock is 200Hz or 5ms/tick. Skip 11 ticks to match the
  173.      * definition in timer.h (MSPTICK = 55 there.) -- hyc
  174.      */
  175.     if((clock() - clkval) > 10) {
  176.         clkval = clock();
  177.         icmpclk();
  178.         tick();
  179.         (void)iss();
  180.     }
  181. }
  182. #endif
  183.  
  184. /* Initialize async port "dev" (adapted from PE1CHL) */
  185. int
  186. asy_init(dev,addr,vec,bufsize)
  187. int16 dev;
  188. char *addr, *vec;
  189. unsigned bufsize;
  190. {
  191.     register struct iorec *ip;
  192.     register struct asy *ap;
  193.     char *bufp;
  194.  
  195. #ifdef DEBUG
  196.     printf("asy_init: dev=%d bufsize=%d\n",dev,bufsize);
  197.     fflush(stdout);
  198. #endif
  199.     ap = &asy[dev];
  200.  
  201. /* ---- Moved here from slip.c - originally by DG2KK... -- hyc ----- */
  202.     /* addr    (COM Port address) is the Atari device name
  203.      *         (either "AUX:" or "MIDI")
  204.      * vec    (Interrupt vector) is used as a flag to indicate if 
  205.      *         bytes received on that interface should be sent out on
  206.      *         another interface (1 = AUX: 3 = MIDI).
  207.      */
  208.     ap->vec = atoi(vec);        /* dev to resend bytes to */
  209.     ap->addr = 0;            /* use as error flag */
  210.     if (strcmp(addr,"AUX:") == 0) {
  211.         ap->addr = 1;
  212.     } else if (strcmp(addr,"CON:") == 0) {    /* This would be stupid. */
  213.         ap->addr = 2;
  214.     } else if (strcmp(addr,"MIDI") == 0) {
  215.         ap->addr = 3;
  216.     }
  217. /* ----- end of paraphrase of slip.c... -- hyc ------- */
  218.     if (ap->addr == 0) {
  219.         printf("asy_init(%d): unknown interface\n",dev);
  220.         return -1;
  221.     }
  222.  
  223.     /* force user to allocate more memory than he already has.
  224.      * If no memory is allocated, asy_stop() may behave funny...
  225.      */
  226.     if (bufsize <= 256)    /* only allocate a bigger buffer */
  227.         return -1;
  228.  
  229.     if ((bufp = malloc(bufsize)) == NULLCHAR){
  230.         printf("asy_init(%d): no memory for rx buffer\n",dev);
  231.         return -1;
  232.     }
  233.  
  234.     /* Save original IOREC values */
  235.  
  236.     ip = Iorec((ap->addr)-1);    /* Iorec wants AUX: = 0, MIDI = 2 */
  237.  
  238.     Jdisint(12);            /* disable RS-232 interrupt */
  239.  
  240.     ap->in = ip;
  241.     memcpy(&ap->oldin,ip);
  242.     if (ap->addr == RS232) {    /* increase RS-232 transmit buffer? */
  243.         ip++;
  244.         ap->out = ip;
  245.         memcpy(&ap->oldout,ip);
  246.     }
  247.  
  248.     /* Set up receiver FIFO */
  249.     ap->in->ibuf = bufp;
  250.     ap->in->ibufsiz = bufsize;
  251.     ap->in->ibufhd = ap->in->ibuftl = 0;
  252.     ap->in->ibuflow = 0;
  253.     ap->in->ibufhi = bufsize;
  254.  
  255.     if (ap->addr == RS232) {
  256.         /* clear transmitter FIFO */
  257.         ap->out->ibufhd = ap->out->ibuftl = 0;
  258.         ap->out->ibuflow = 0;
  259.         ap->out->ibufhi = ap->out->ibufsiz;
  260.     }
  261.  
  262.     Jenabint(12);            /* enable RS-232 interrupts */
  263.  
  264.     if (ap->addr == RS232)
  265.         Rsconf(-1,0,-1,0,0,0);    /* 8 bits, no parity */
  266.  
  267. #ifdef DEBUG
  268.     printf("asy_init: Iorecs in: 0x%lx out: 0x%lx\n",ap->in,ap->out);
  269.     printf("    inbuf: 0x%lx outbuf: 0x%lx\n",ap->in->ibuf,
  270.         ap->out->ibuf);
  271. #endif
  272. }
  273.  
  274.  
  275. /* asy_stop restores old iorec and frees memory allocated to the RS-232/MIDI
  276.  * buffers. (from PE1CHL)
  277.  */
  278. int
  279. asy_stop(iface)
  280. struct interface *iface;
  281. {
  282.     register struct asy *ap;
  283. /*    char i_state; */
  284.  
  285. #ifdef DEBUG
  286.     printf("asy_stop: iface=0x%lx dev=%d\n",iface,iface->dev);
  287.     fflush(stdout);
  288. #endif
  289.     ap = &asy[iface->dev];
  290.  
  291.     (void) Jdisint(12);        /* disable RS-232 interrupts */
  292.  
  293.     free(ap->in->ibuf);        /* free the buffer */
  294.  
  295.     /* Restore old iorecs */
  296.     memcpy(ap->in,&ap->oldin,sizeof(struct iorec));
  297.     if (ap->addr == RS232)
  298.         memcpy(ap->out,&ap->oldout,sizeof(struct iorec));
  299.  
  300.     (void) Jenabint(12);        /* enable RS-232 interrupts */ 
  301. }
  302.  
  303.  
  304. /* Set async line speed */
  305. int
  306. asy_speed(dev,speed)
  307. int dev;
  308. int speed;
  309. {
  310.     int baud; /* int result; */
  311.     register int sp;
  312.     long sav_ssp;
  313.  
  314.     if (speed <= 0 || dev >= nasy)
  315.         return -1;
  316.  
  317.     asy[dev].speed = speed;        /* shouldn't this be done in slip.c? */
  318.  
  319.     switch (asy[dev].addr) {
  320.  
  321.     case RS232:
  322.         switch (speed) {
  323.         case 300:
  324.             baud = 9;    /* how slow can you get? :-) */
  325.             break;
  326.         case 1200:
  327.             baud = 7;
  328.             break;
  329.         case 2400:
  330.             baud = 4;
  331.             break;
  332.         case 4800:
  333.             baud = 2;
  334.             break;
  335.         case 9600:
  336.             baud = 1;
  337.             break;
  338.         case 19200:
  339.             baud = 0;
  340.             break;
  341.         default:
  342.             printf("asy_speed: unknown RS-232 speed (%d).\n",speed);
  343.             return -1;
  344.         }
  345.         (void) Rsconf(baud,0,0x88,-1,-1,-1);    /* no flow control */
  346.         break;
  347.  
  348.     case MIDI:
  349.         /* midi can run on 500000 or 614400Hz clock (hardware mod)
  350.          */
  351.         switch (speed)
  352.         {
  353.         case 0x9600:        /* = 38400 unsigned... (hardware mod) */
  354.         case 31250:        /* normal MIDI baudrate */
  355.             sp = 0x95;    /* /16 */
  356.             break;
  357.  
  358.         case 9600:        /* 9600 Baud requires hardware mod */
  359.         case 7812:        /* 7812 Baud on an unmodified Atari */
  360.             sp = 0x96;    /* /64 */
  361.             break;
  362.  
  363.         default:
  364.             printf("asy_speed: unknown MIDI speed (%d).\n",speed);
  365.             return -1;
  366.         }
  367.  
  368.         sav_ssp = Super(NULL);        /* switch to Supervisor mode */
  369.         *((char *) 0xfffffc04L) = 0x03; /* Reset 6850 */
  370.         hiword(0);            /* spend some time */
  371.         *((char *) 0xfffffc04L) = sp;    /* set new divider ratio */
  372.         Super(sav_ssp);            /* back to User mode */
  373.  
  374.         break;
  375.     }
  376.     asy_flush(dev);
  377. }
  378.  
  379.  
  380. /* flush the input buffer of device dev (either aux: or midi).
  381.  * May be useful, because setting the baudrate causes an 0x7f to be sent.
  382.  */
  383. asy_flush(dev)
  384. int dev;
  385. {
  386.     int st_dev;
  387.     long c;                    /* Bconin returns a long */
  388.     st_dev = asy[dev].addr;
  389.  
  390.     while (Bconstat(st_dev) == -1) {    /* at least 1 char avlb */
  391.         c = Bconin(st_dev);
  392.     }
  393. }
  394.  
  395.  
  396. /* Send a buffer to serial transmitter */
  397. asy_output(dev,buf,cnt)
  398. unsigned dev;
  399. char *buf;
  400. unsigned short cnt;
  401. {
  402.     int st_dev;
  403.     unsigned short i = 0;
  404.  
  405.     if (dev >= nasy)
  406.         return -1;
  407.  
  408.     st_dev = asy[dev].addr;
  409.     for (i = 0 ; i < cnt ; ++i) {
  410.         (void)Bconout(st_dev,buf[i]);    /* 1= AUX: 3=MIDI: */
  411.     }
  412. }
  413.  
  414.  
  415. /* Receive characters from async line
  416.  * Returns count of characters received
  417.  * dev is the device 
  418.  * buf is the buffer where received characters are stored
  419.  * cnt is the number of characters the calling routine expects to find in buf.
  420.  * On return, the no of chars still in the rs-232 buffer should be returned!??
  421.  * Quick and dirty hack: 
  422.  * since this routine is called from one location (slip.c) only, with cnt=1
  423.  * we just return the AUX: state (char avlb or not (bios(1,1) bconstat)).
  424.  * 
  425.  */
  426. unsigned
  427. asy_recv(dev,buf,cnt)
  428. int dev;
  429. char *buf;
  430. unsigned cnt;
  431. {
  432.     char c;
  433.     int rs_state;
  434.     int st_dev = asy[dev].addr;    /* AUX: device on the Atari ST */
  435.     int snd_dev = asy[dev].vec;
  436.  
  437.     rs_state = Bconstat(st_dev);    /* 0 = no char, -1 = char avlb */
  438.  
  439.     if (rs_state == -1) {
  440.         rs_state = 1;        /* at least one char available */
  441.         c = (char)Bconin(st_dev);
  442. #ifdef    RETRANSMIT
  443.         /* this does the actual retransmitting of received bytes
  444.          * depending on the vector field in the 'attach asy' command.
  445.          * remove it, if you don't have use for it.
  446.          */
  447.         if (snd_dev == 1 || snd_dev == 3) {    /* if AUX: or MIDI */
  448.             (void)Bconout(snd_dev,c & 0xff);
  449.         }
  450. #endif
  451.         *buf = c & 0xff;
  452.     }
  453.     return (rs_state);    /* 0 = no char, 1 = at least 1 more char */
  454. }
  455.  
  456. #ifdef    MWC
  457. int                /* Just for argument's sake... -- hyc */
  458. kbhit()
  459. {
  460.     return(Cconis());
  461. }
  462. #endif
  463.  
  464. int kbread()
  465. {
  466.     int c;
  467.     long    raw;
  468.  
  469.     if (Cconis() == 0){    /* no key hit: just return */
  470.         return (-1);
  471.     }
  472.  
  473.     raw = Crawcin();    /* Read ASCII and scan code from keyboard */
  474.     c = raw & 0x7f;        /* get rid of scan codes !!!! */
  475.  
  476. #ifdef    ESCAPEOUT        /* Why would you *ever* do this???  -- hyc */
  477.     if (c == 0x1b)
  478.         c = -2;        /* ESCAPE behaves like F10 */
  479. #endif
  480.     /* process function keys */
  481.     if (c == 0) {        /* we have an F key */
  482.         switch((int) ((raw >> 16) & 0xff)) {
  483.         case 0x3b:            /* F1 */
  484.             toggle = ~toggle;
  485.             (void) dispscreen(toggle);
  486.             c = -1;
  487.             break;
  488.  
  489.         case 0x3c:            /* F2 */
  490.             printtrace = 0;        /* printer off */
  491.             c = -1;
  492.             break;
  493.  
  494.         case 0x55:            /* Shift F2 */
  495.             printtrace = 1;        /* printer on */
  496.             c = -1;
  497.             break;
  498.  
  499.         case 0x44:            /* F10 */
  500.             c = -2;
  501.             break;
  502.         default:
  503.             c = -1;            /* ignore other keys */
  504.             break;
  505.         }
  506.     }
  507.     return (c);
  508. }
  509.  
  510. #ifndef    MWC        /* see vfile.c for MWC -- hyc */
  511. /* Create a temporary file and return the filepointer
  512.  * (adapted from the routine in mac_io.c)
  513.  */
  514. FILE *
  515. tmpfile()
  516. {
  517.     FILE *tmp;
  518.     char    name[32];
  519.     struct    TempList *tptr;
  520.     long tt;
  521.  
  522.     tt = clksec();
  523.     tt &= 0x007fffffL;
  524.     sprintf(name,"\\X%07.7ld.tmp",tt);
  525.     if ((tmp = fopen(name,"wrb")) == NULL) {
  526.         printf("tmpfile: could not create temp file. (%s)\n",name);
  527.         return(tmp);
  528.     }
  529.     tptr = (struct TempList *)malloc(sizeof(struct TempList));
  530.     if (tptr == NULL) {
  531.         printf("tmpfile: not enough memory for TempList\n");
  532.         return(tptr);
  533.     }
  534.     if(Head != NULL)
  535.         tptr->next = Head->next;
  536.     else
  537.         tptr->next = NULL;
  538.     if((tptr->name = malloc(strlen(name)+1)) == NULL) {
  539.         printf("tmpfile: not enough memory for name\n");
  540.         return(tptr);
  541.     }
  542.     strcpy(tptr->name,name);
  543.     if(Head != NULL)
  544.         Head->next = tptr;
  545.     else
  546.         Head = tptr;
  547.     return (tmp);
  548. }
  549. #endif
  550.  
  551. #ifndef    disable
  552. int disable()
  553. {
  554.     return 1;
  555. }
  556.  
  557. int restore(istate)
  558. int istate;
  559. {
  560. }
  561. #endif
  562.  
  563. void eihalt()
  564. {
  565. }
  566.  
  567.  
  568. /* the following is new stuff from version 871225.1 */
  569.  
  570. asy_ioctl(interface,argc,argv)
  571. struct interface *interface;
  572. int argc;
  573. char * argv[];
  574. {
  575.     if (argc < 1) {
  576.         printf("speed: %d baud\n",asy[interface->dev].speed);
  577.         return 0;
  578.     }
  579.     return asy_speed(interface->dev,atoi(argv[0]));
  580. }
  581.  
  582. int asy_rxint()
  583. {
  584.     printf("asy_rxint\n");
  585. }
  586.  
  587. int asy_txint()
  588. {
  589.     printf("asy_txint\n");
  590. }
  591.  
  592. int setbit()
  593. {
  594.     printf("setbit\n");
  595. }
  596.  
  597. int clrbit()
  598. {
  599.     printf("clrbit\n");
  600. }
  601.  
  602. void
  603. rename(s,d)
  604.     char *s, *d;        /* Source and dest name */
  605. {
  606.     Frename(0, s, d);    /* Ignore return code... */
  607. }
  608.  
  609. doshell(argc,argv)
  610. int argc; char *argv[];
  611. {
  612.     long execstat;
  613.     char *args[2];
  614.  
  615. #ifdef SCREEN
  616.     /* make sure that we are on the right (main) screen */
  617.     dispscreen(0);
  618.     toggle = 0;
  619. #endif
  620.     if (argc < 2) {
  621.         args[0]="shell";
  622.         args[1]=NULL;
  623.         execstat = exec(shell,args);
  624.     } else
  625.         execstat = exec(argv[1],&argv[1]);
  626.  
  627.     if (execstat != 0)
  628.         printf("Pexec: errorcode: %ld\n",execstat);
  629.  
  630.     printf("\n");
  631.     (void)Cursconf(1,0);        /* cursor on */
  632.     (void)Cursconf(2,0);        /* cursor blink */
  633. }
  634.  
  635.  
  636. dotype(argc,argv)
  637. int argc;
  638. char *argv[];
  639. {
  640.     char tstring[200];
  641.     char fname[50];
  642.     FILE *tfile;
  643.     int linecnt = 0;
  644.  
  645.     if (argc != 2) {
  646.         printf("Usage: type <filename>\n");
  647.         return;
  648.     }
  649.  
  650.     strcpy(fname,argv[1]);
  651.     if ((tfile = fopen(fname, "r")) == NULLFILE) {
  652.         printf("type: cannot open '%s'\n",fname);
  653.         return;
  654.     }
  655.  
  656.     while (!feof(tfile)) {
  657.         fgets(tstring,200,tfile);
  658.         linecnt++;
  659.         if (strlen(tstring) > 79)
  660.             linecnt++;
  661.         printf("%s",tstring);
  662.         if ((linecnt % 23) == 0) {
  663.             printf("\033p more \033q");
  664.             if ((gemdos(7) & 0x7f) == 'q') {
  665.                 printf("\033l\n");
  666.                 break;
  667.             }
  668.             printf("\033l");
  669.         }
  670.     }
  671.     fclose(tfile);
  672. }
  673.  
  674. giveup()
  675. {
  676. }
  677.  
  678. int stxrdy(dev)
  679. int dev;
  680. {
  681.     int stat;
  682.     int st_dev;
  683.  
  684.     st_dev = asy[dev].addr;
  685.     stat=(int)Bcostat(st_dev);    /* Bcostat:  -1 ready , 0 not ready */
  686.     if (stat == -1) {
  687.         stat = 1;
  688.     }
  689.     return (stat);
  690. }
  691.  
  692. #ifdef    LATTICE
  693. /*
  694.  * replacement for buggy Lattice memchr function used in telnet.c
  695.  */
  696. char *
  697. memchr_st(str,c,n)
  698. char *str;
  699. char c;
  700. unsigned n;
  701. {
  702.     while (n-- != 0) {
  703.         if (*str++ == c)
  704.             return (str);
  705.     }
  706.     return NULLCHAR;
  707. }
  708.  
  709.  
  710. /* move (copy) a block of memory */
  711. /* this function assumes the blocks do not overlap */
  712. memcpy (d,s,c)
  713. register char *d;            /* destination pointer */
  714. register char *s;            /* source pointer */
  715. register int  c;            /* bytecount */
  716. {
  717.     if (c)                /* nonzero count? */
  718.     do
  719.     {
  720.         *d++ = *s++;        /* then move on */
  721.     } while (--c);            /* while more to go */
  722. }
  723.  
  724. /* fill a block of memory */
  725. void memset (p,c,n)
  726. register char *p;            /* block pointer */
  727. register char c;            /* initialization value */
  728. register int  n;            /* bytecount */
  729. {
  730.     if (n)                /* nonzero count? */
  731.     do
  732.     {
  733.         *p++ = c;        /* then do it */
  734.     } while (--n);            /* more to fill? */
  735. }
  736.  
  737. /* compare memory blocks, return 0 if equal */
  738. int memcmp (d,s,c)
  739. register char *d;            /* destination pointer */
  740. register char *s;            /* source pointer */
  741. register int  c;            /* bytecount */
  742. {
  743.     if (c)                /* nonzero count? */
  744.     do
  745.     {
  746.         if (*s++ != *d++)    /* compare */
  747.         return (*--d - *--s);    /* when unequal, return diff */
  748.     } while (--c);            /* while more to go */
  749.  
  750.     return (0);                /* equal! */
  751. }
  752.  
  753. /* lookup some character in a memory block */
  754. char *memchr (p,c,n)
  755. register char *p;            /* block pointer */
  756. register char c;            /* value to look for */
  757. register int  n;            /* bytecount */
  758. {
  759.     while (n--)
  760.     if (*p == c)
  761.         return (p);
  762.     else
  763.         p++;
  764.  
  765.     return (NULLCHAR);
  766. }
  767. #endif    /* LATTICE */
  768.  
  769. memstat()
  770. {
  771. #ifdef    AX25
  772.     extern int digisent;
  773. #endif
  774.     long size;
  775.  
  776.     size = Malloc(-1L);
  777.     printf("\nFree memory: %ld bytes.\n\n",size);
  778. #ifdef    AX25
  779.     printf("Digisent: %d frames.\n\n",digisent);
  780. #endif
  781.     printf("AUX:");
  782.     iosize(0);
  783.     printf("MIDI:");
  784.     iosize(2);
  785. }
  786.  
  787. iosize(i)
  788. int i;
  789. {
  790.     struct iorec *rsbuffer;
  791.  
  792.     rsbuffer = (struct iorec*) Iorec(i);        /* get iorec */
  793.     printf("\tIorec:  %08lx\n",rsbuffer);
  794.     printf("\tBuffer: %08x\n",rsbuffer->ibuf);
  795.     printf("\tSize:   %d bytes\n",rsbuffer->ibufsiz);
  796. }
  797.  
  798. #ifdef    LATTICE
  799. #define    DMABUFFER    struct FILEINFO
  800. #define d_fname        name
  801.  
  802. /* delete all temp files which have accumulated */
  803. tmpdel()
  804. {
  805.     DMABUFFER info;
  806.     char delnam[20];
  807.     int error;
  808.  
  809.     Fsetdta(&info);
  810.  
  811.     if ((error = Fsfirst("\\x*.tmp",0)) != 0){
  812.         info.d_fname[0] = '\0';
  813.     }
  814.     while (info.d_fname[0] != '\0') {
  815.         sprintf(delnam,"\\%s",info.d_fname);
  816.         unlink(delnam);
  817.         if ((error = Fsnext()) != 0) {
  818.             info.d_fname[0] = '\0';
  819.         }
  820.     }
  821. }
  822.  
  823.     /* The MWC version seems OK. -- hyc */
  824. /* the access() call doesn't seem to work in Lattice-C.
  825.  * this is a quick and dirty hack...
  826.  * it returns -1 if the file exists and 0 if it doesn't.
  827.  */
  828. int
  829. access(name,mode)
  830. char *name;
  831. int mode;
  832. {
  833.     struct FILEINFO *info;
  834.     int ret;
  835.  
  836.     Fsetdta(&info);
  837.     if ((ret = Fsfirst(name,0)) != 0)
  838.         return -1;
  839.     return 0;
  840. }
  841. #endif    /* LATTICE */
  842.  
  843. #ifdef SCREEN
  844.  
  845. /* dispscreen selects the screen to be displayed by the video hardware.
  846.  * this routine is called from within kbread()
  847.  * flag:  =0: display normal screen
  848.  *       <>0: display trace screen
  849.  */
  850. dispscreen(flag)
  851. int flag;
  852. {
  853.     if (flag == 0) {
  854.         (void) Setscreen(-1L,physcreen,-1);
  855.         (void) Vsync();
  856.         printf("\033e");    /* turn on cursor */
  857.     } else {
  858.         (void) Setscreen(-1L,newscreen,-1);
  859.         (void) Vsync();
  860.         printf("\033f");    /* turn off cursor */
  861.     }
  862. }
  863.  
  864.  
  865. /* outscreen selects one of two screens where display output (printf's) should
  866.  * go to.
  867.  * outscreen is called from trace.c before/after trace output is done
  868.  */
  869. outscreen(flag)
  870. int flag;
  871. {
  872.     if (flag == 0) {
  873.         (void) Setscreen(logscreen,-1L,-1);
  874.         (void) Vsync();
  875.         printf("\033k");    /* restore cursor pos on main screen */
  876.     } else {
  877.         (void) Setscreen(newscreen,-1L,-1);
  878.         (void) Vsync();
  879.         printf("\033j");    /* save cursor pos on main screen */
  880.         printf("\033Y%c%c",rows+31,32);
  881.     }
  882. }
  883. #endif
  884.  
  885. #ifdef    MWC
  886. static exec(command,args)
  887. char *command;
  888. char *args[];
  889. {
  890.     char **envx,*pt,*com;
  891.     register int i,j,k,l;
  892.  
  893.     pt=getenv("PATH");        /* Find an executable file named */
  894.     if(pt == NULL)            /* command in the path... */
  895.         pt=DEFPATH;
  896.  
  897.     com=path(pt,command,1);
  898.     if(com != NULL)
  899.         command = com;
  900.  
  901.     j=(-1);
  902.     for(i=0;environ[i]!=0;i++)    /* Count vars in environment */
  903.         if(!strncmp(environ[i],"ARGV",4))    /* Skip ARGV */
  904.             j=i;
  905.  
  906.     if(j<0)
  907.         i++;
  908.  
  909.     envx=(char **)malloc(i*sizeof(char *));    /* Copy environment */
  910.     i--;
  911.     envx[i] = NULL;
  912.  
  913.     for(k=0,l=0;k<i;l++)
  914.         if(l!=j) {
  915.             envx[k]=malloc(strlen(environ[l])+1);
  916.             strcpy(envx[k],environ[l]);
  917.             k++;
  918.         }
  919.     
  920.     return(execve(command,args,envx));
  921. }
  922. #endif
  923.     
  924. /* Do a warm boot, I think. -- hyc */
  925. void
  926. sysreset()
  927. {
  928.     long *resvec,(*resjump)();
  929.  
  930.     Super(NULL);    /* Get into supervisor mode to read sys vars */
  931.     resvec = (long *)0x42a;
  932.     resjump = *resvec;
  933.     (*resjump)();
  934. }
  935.  
  936. #ifdef    MWC
  937. /*
  938.  * set console output to binary or ascii mode (raw or cooked...)
  939.  * mode == 1 means raw, anything else means cooked.
  940.  * This is a compiler/library dependent hack. Too bad I can't
  941.  * come up with a better way.  -- hyc
  942.  */
  943.  
  944. void
  945. set_stdout(mode)
  946. int mode;
  947. {
  948.     FILE *fp, *_fopen();
  949.     char *fmode;
  950.  
  951.     if (mode == 1)
  952.         fmode = "wb";
  953.     else
  954.         fmode = "w";
  955.  
  956.     fflush(stdout);
  957.     fp=_fopen(NULL, fmode, stdout, 1);
  958. }
  959. #endif
  960.